package net.ojbk.study.curriculumDesign.web.listener;

import net.ojbk.study.curriculumDesign.beans.canvasmodel.Canvas;
import net.ojbk.study.curriculumDesign.beans.sall.Sall;
import net.ojbk.study.curriculumDesign.dao.canvasmodel.CanvasDao;
import net.ojbk.study.curriculumDesign.service.sall.ISallService;
import net.ojbk.study.curriculumDesign.util.Changeright;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@WebListener
public class ApplicationListener implements ServletContextListener {
    // 学到了学到了，WebListerner
    @Autowired
    @Qualifier("sallService")
    private ISallService sallService;
    @Autowired
    @Qualifier("canvasDao")
    private CanvasDao canvasDao;

    public ApplicationListener() {
    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext context=sce.getServletContext();
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
//        sallService.maintenancedata(new Date());
        Changeright changeright=new Changeright(sallService);
        Thread thread=new Thread(changeright);
        thread.start();
        List<Sall> sallList=sallService.getAllSall();
        for (Sall s:sallList
             ) {//test
            System.out.println("正在拍卖的油画id："+s.getCid());
        }
        context.setAttribute("salls",sallList);
        Set<Canvas> canvasSet=new HashSet<>();
        for (Sall sall:sallList) {
            System.out.println(sall.getCid());//test
            Canvas canvas=null;
            canvas=canvasDao.selectOneCanvas(sall.getCid());
            System.out.println(canvas.getName());//test
            canvasSet.add(canvas);
        }
        context.setAttribute("canvasOnSall",canvasSet);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}
